<?
  define('MYSQL_CONNECTION_CLOSED',0);
  define('MYSQL_CONNECTION_OPENED',1);
  

  class CMySQL
  {
  
   private $hostname;
   private $username;
   private $password;
   private $database;
   private $query;
   private $connectionState;
   private $connectionId;
   
   public function __construct($hostname, $username, $password, $database)
   {
    if(!$hostname && !$username && !$password && !$database)
     throw new Exception('MySQL Data incomplete!');
     
    $this->hostname = $hostname;
    $this->username = $username;
    $this->password = $password;
    $this->database = $database;
    
    $this->connectionState = MYSQL_CONNECTION_CLOSED;
   }
  
   public function connect()
   {
    if($this->connectionState == MYSQL_CONNECTION_CLOSED)
    {
     $this->connectionId = @mysql_connect($this->hostname, $this->username, $this->password);
     
     if(empty($this->connectionId) || $this->connectionId ==  false)
      throw new Exception('MySQL Connection failed: '.mysql_error());


     if(@mysql_select_db($this->database, $this->connectionId))
      $this->connectionState = MYSQL_CONNECTION_OPENED;
     else
      throw new Exception('MySQL DB select failed: '.mysql_error());
    }

    return true;
   }
  
  
   public function close()
   {
    if($this->connectionState == MYSQL_CONNECTION_OPENED)
     @mysql_close();
   }
  
  
   public function query($queryString)
   {
    if($this->connect())
     return new CMySQLQuery($queryString);
   }
   
  }
  
  
  class CMySQLQuery
  {

   private $numRows;
   private $queryId = null;
   
   public function __construct($queryString)
   {
    $this->queryId = @mysql_query($queryString);
    if(empty($this->queryId) || $this->queryId == false)
     throw new Exception('MySQL Query failed: '.mysql_error());
   }
   
   public function numRows()
   {
    if($this->numRows == null)
     $this->numRows = mysql_num_rows($this->queryId);
     
    return $this->numRows;
   }
   
   public function fetchArray()
   {
    return mysql_fetch_array($this->queryId);
   }
  }
  
?>